package com.cch.cooperation.api.common.util;

/**
 * 数字工具类
 *
 * @author cch on 2023/04/23
 */
public class NumUtil {

    private static final char[] BASE_32_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    /**
     * 10 进制转 32 进制
     *
     * @param num 十进制数
     * @return 32 进制
     */
    public static String base10To32(Long num) {
        if (num < 1) {
            return "0";
        }
        StringBuilder builder = new StringBuilder();
        while (num > 0) {
            int idx = (int) (num % 32);
            builder.append(BASE_32_CHARS[idx]);
            num /= 32;
        }
        return builder.reverse().toString();
    }

    /**
     * 32 进制转 10 进制
     *
     * @param num 32进制数
     * @return 10 进制数
     */
    public static Long base32To10(String num) {
        int[] charMap = new int[126];
        for (int i = 0; i < BASE_32_CHARS.length; i++) {
            charMap[BASE_32_CHARS[i]] = i;
        }
        long result = 0;
        for (char c : num.toCharArray()) {
            result = result * 32 + charMap[c];
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(base10To32(1846003732546985984L));
    }

}
